package com.brsya.movie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.brsya.base.base.BaseLayoutActivity;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.bean.PlayHistoryBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.recycler.BaseViewHolder;
import com.brsya.base.util.GlideUtil;
import com.brsya.base.util.StringUtil;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.application.MyApplication;
import com.brsya.movie.widget.ScoreView;

import java.util.List;


/**
 * Created by Brsya
 * CreateDate: 2022/5/28  18:28
 * Description:
 * 播放历史记录
 */
public class PlayHistoryActivity extends BaseLayoutActivity {


    private RelativeLayout llPageTitleBox;
    private TextView tvPageTitle;
    private LinearLayout llTitleBackBox;
    private RecyclerView rlPlayHistory;

    private BaseAdapter<PlayHistoryBean> adapter;


    private List<PlayHistoryBean> playHistories;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_history);
        setDarkStatus();

        initView();

        initViewData();
    }

    private void initView() {
        llPageTitleBox = findViewById(R.id.ll_page_title_box);
        tvPageTitle = findViewById(R.id.tv_page_title);
        llTitleBackBox = findViewById(R.id.ll_title_back_box);
        rlPlayHistory = findViewById(R.id.rl_play_history);
    }

    private void initViewData() {
        UiUtil.setStatusPadding(this,llPageTitleBox);

        llTitleBackBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        adapter = new BaseAdapter<PlayHistoryBean>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(PlayHistoryActivity.this).inflate(R.layout.item_play_history, parent, false);
                return new BaseViewHolder(itemView);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, PlayHistoryBean data) {
                ImageView ivMovieImg = viewHolder.findViewById(R.id.iv_movie_img);
                TextView tvMovieTitle = viewHolder.findViewById(R.id.tv_movie_title);
                ScoreView svScoreBox = viewHolder.findViewById(R.id.sv_score_box);
                TextView tvMoviePlayTime = viewHolder.findViewById(R.id.tv_movie_play_time);
                TextView tvMoviePlayProgress = viewHolder.findViewById(R.id.tv_movie_play_progress);

                MovieBean movieBean = data.getMovieBean();

                if (movieBean == null) {
                    return;
                }

                GlideUtil.loadRound(ivMovieImg, movieBean.getCoverUrl(), 4);
                tvMovieTitle.setText(movieBean.getName());
                if (!StringUtil.isEmpty(movieBean.getDoubanScore())) {
                    svScoreBox.setScore(movieBean.getDoubanScore());
                }
                tvMoviePlayTime.setText(getResources().getString(R.string.play_history_time, StringUtil.generateDataOrTime(data.getSaveTime())));
                if (data.getPartName() == null) {
                    tvMoviePlayProgress.setText(getResources().getString(R.string.play_history_progress, StringUtil.generateTime(data.getPlayTime())));
                }else{
                    tvMoviePlayProgress.setText(
                            getResources().getString(
                                    R.string.play_history_progress_part
                                    , data.getPartName()
                                    , StringUtil.generateTime(data.getPlayTime())));
                }
            }
        };

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<PlayHistoryBean>() {
            @Override
            public void onItemClick(int position, PlayHistoryBean data) {
                // TODO: 2022/7/6 跳转到播放页面
                Intent intent = new Intent(PlayHistoryActivity.this, PlayActivity.class);
                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, data.getMovieBean());
                startActivity(intent);
            }
        });

        View bottomView = LayoutInflater.from(this).inflate(R.layout.item_no_more_data, null, false);
        adapter.setBottomView(bottomView);

        rlPlayHistory.setLayoutManager(new LinearLayoutManager(this));
        rlPlayHistory.setAdapter(adapter);

        String playHistoriesStr = MyApplication.aCache.getAsString(Contents.CACHE_KEY_HISTORY);
        if (!StringUtil.isEmpty(playHistoriesStr)) {
            playHistories = JSON.parseArray(playHistoriesStr, PlayHistoryBean.class);
            adapter.setDataList(playHistories);
            adapter.notifyDataSetChanged();
        }

    }
}
